/**
 * Copyright (c) 2012 Sean Beecroft, Permission is hereby granted, free of
 * charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * For more information on this project and others, please visit my google code 
 * repository:
 * https://code.google.com/u/seanbeecroft@gmail.com/
 */
package javaballs;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.TimerTask;
import java.util.Vector;
import javax.swing.JPanel;

public class G extends JPanel implements MouseMotionListener, MouseListener {

    Image buffer = null;
    Image clear = null;

    public G() {
    }

    public void clear() {
        clear = null;
        buffer = null;
    }

    @Override
    public void paint(Graphics g) {
        if (buffer == null || clear == null) {
            buffer = this.createImage(Globals.app.getWidth(), Globals.app.getHeight());
            clear = this.createImage(Globals.app.getWidth(), Globals.app.getHeight());
            clear.getGraphics().fillRect(0, 0, Globals.app.getWidth(), Globals.app.getHeight());
        }
        Graphics bg = buffer.getGraphics();
        bg.drawImage(clear, 0, 0, this);
        ArrayList<Ball> balls = Main.getBalls();
        for (int i = 0; i < balls.size(); i++) {
            Ball b = (Ball) balls.get(i);
            bg.setColor(b.getColor());
            if (b != null) {
                bg.fillOval((int) b.getX(), (int) b.getY(), b.getWidth(), b.getWidth());
            }
        }

        g.drawImage(buffer, 0, 0, this);
        g.dispose();

    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        Ball ball = new Ball();
        ball.setX((double) x);
        ball.setY((double) y);
        ball.setVx(5);
        ball.setVy(6);
        double v = (Math.random() * 50) + 10;
        ball.setWidth((int) v);
        ball.setMass((int) v);

        ball.setColor(Utility.getRandomColor());
        Main.getBalls().add(ball);
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
}

class CollisionDetectionTask extends TimerTask {
    int n = 0;
    public void run() {
        ArrayList<Ball> balls = Main.getBalls();
        for (int i = 0; i < balls.size(); i++) {
            Ball b = (Ball) balls.get(i);
            if (b != null) {
                ArrayList<Ball> l = b.getCollisions();
                if( l.isEmpty() ){
                    b.collision = false;
                }
                for (int j = 0; j < l.size(); j++) {
                    Ball m = l.get(j);
                    b.hit(m);
                    b.collision = true;
                    m.collision = true;
                }
            }
        }
    }
}
